package P2PChat;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends Frame {
    JLabel lusername = new JLabel("用户名");
    JLabel lpassword = new JLabel("密码");
    //文本框
    TextField tusername = new TextField(10);
    //密码框
    TextField tpassword = new TextField(10);
    //登陆时的确定按钮
    JButton bok = new JButton("登陆");
    JButton bcancle = new JButton("取消");
    //对两个按钮进行监听，点击就输出ok、cancel
    public LoginFrame() throws HeadlessException {

        //对界面进行排版
        this.setBounds(400, 200, 300, 160);
        this.setLayout(new GridLayout(3, 1, 5, 5));//布局，切成三行一列
        tpassword.setEchoChar('*');//对密码框进行星号*显示
        bok.addActionListener(new LoginListener());
        bcancle.addActionListener(new LoginListener());
        //为三行，每一行都设置一个Panel
        Panel upPanel = new Panel();
        Panel cenPanel = new Panel();
        Panel downPanel = new Panel();
        upPanel.add(lusername);
        upPanel.add(tusername);
        cenPanel.add(lpassword);
        cenPanel.add(tpassword);
        downPanel.add(bok);
        downPanel.add(bcancle);
        this.add(upPanel);
        this.add(cenPanel);
        this.add(downPanel);
        this.setTitle("登录界面");
        //添加WindowEventListener(独立写的关闭)类
        this.addWindowListener(new WindowEventListener());
        //显示出来
        this.setVisible(true);
    }
    //监听类是登陆界面的内部类
    class LoginListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == bok) {//对监听类e进行判断，若为登陆按钮，
//                    System.out.println("ok");
                String username=tusername.getText();
                String password=tpassword.getText();
                //对用户名和密码进行判断
                boolean flag=username!=null&&password!=null&&
                        username.equals("123")&&password.equals("123");
                if(flag){
                    try {
                       Data.mf.setVisible(true);//显示聊天窗口
                    } catch (Exception exception) {
                        exception.printStackTrace();
                    }
                    //外围类  .this
                    LoginFrame.this.setVisible(false);
                }
            } else if (e.getSource() == bcancle) {//对监听类e进行判断，若为取消按钮，
//                    System.out.println("cancle");
                tusername.setText("");
                tpassword.setText("");
            }
        }
    }
}
